#include "Timer.h"

using namespace GUIToolkit;

map<UINT, Timer*> Timer::timers;

Timer::Timer(void)
	: elapse(0), iTimerID(0)
{
}

void CALLBACK Timer::TimerProc( HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
	auto it = timers.find(iTimerID);
	if(it != timers.end()) it->second->OnTick();
}

void Timer::Start(UINT elapse)
{
	iTimerID = ::SetTimer(NULL, 0, elapse, TimerProc);
	if(iTimerID)
	{
		this->elapse = elapse;
		timers[iTimerID] = this;
	}
}

void Timer::Stop()
{
	::KillTimer(NULL, iTimerID);
	timers.erase(iTimerID);
	this->elapse = 0;
	this->iTimerID = 0;
}

Timer::~Timer(void)
{
	if(iTimerID) this->Stop();
}